SHELL := /bin/bash
pwd :="$(shell pwd)"
install : install_flag = true
install_flag ?= false
target ?= "none"
all: handle_manifests handle_target
	@:
install: all
	@exit 0
build: all
	@exit 0
handle_target:
	@if [[ "$(target)" != "none" ]];then\
		if [[ -d "build/$(target)" ]];then\
			cd build/$(target);\
			make;\
		else\
			echo "Error: invalid target '$(target)'";\
			exit 1;\
		fi;\
	fi;
handle_manifests:
	@root=$(root);\
	sed -e '/^#/d' manifest | while read line; do\
		if [[ $$line != ""  ]];then\
			echo $$line;\
			entrytype=`echo $$line | awk '{print $$1}'`;\
			type=`echo $$line | awk '{print $$2}'`;\
			manifest=`echo $$line | awk '{print $$3}'`;\
			case "$$entrytype" in\
				manifest)\
					case "$$type" in \
						filesystem)\
							if $(install_flag); then\
								sed -e '/^#/d' $$manifest | while read line2; do\
									arg=( $$line2 );\
									cp -R $${arg[1]} $(prefix)/$${arg[2]};\
								done;\
							fi;\
						;;\
						build)\
							mdir="`dirname $$manifest`/";\
							i=0;\
							sed -e '/^#/d' $$manifest | while read line; do\
								i=`expr $$i + 1`;\
								if [[ "$$line" != "" ]]; then\
									type=`echo $$line | awk '{print $$2}'`;\
									case "$$type" in \
										build)\
											if ! $(install_flag); then\
												cmd=`echo $$line | awk '{print $$3}'`;\
												if [[ "$$cmd" == "make" ]];then\
													cmd=$cmd" prefix=$(prefix)";\
												fi;\
												cmd=$$cmd`echo $$line | awk '{$$1="";$$2="";$$3="";print $$0}'`;\
												dir=`echo $$line | awk '{print $$1}'`;\
												echo "	Entering: $$dir";\
												cd $$dir;\
												$$cmd;\
											fi;\
										;;\
										install)\
											if $(install_flag); then\
												cmd=`echo $$line | awk '{print $$3}'`;\
												if [[ "$$cmd" == "make" ]];then\
													cmd=$cmd" prefix=$(prefix)";\
												fi;\
												cmd=$$cmd`echo $$line | awk '{$$1="";$$2="";$$3="";print $$0}'`;\
												dir=`echo $$line | awk '{print $$1}'`;\
												echo "	Entering: $$dir";\
												cd $$dir;\
												$$cmd;\
											fi;\
										;;\
										all)\
											cmd=`echo $$line | awk '{print $$3}'`;\
											if [[ "$$cmd" == "make" ]];then\
												cmd=$cmd" prefix=$(prefix)";\
											fi;\
											cmd=$$cmd`echo $$line | awk '{$$1="";$$2="";$$3="";print $$0}'`;\
											dir=`echo $$line | awk '{print $$1}'`;\
											echo "	Entering: $$dir";\
											cd $$dir;\
											$$cmd;\
                                                                                ;;\
                                                                                *)\
                                                                                	echo -e "Error: Invalid build type '$$type'\n\tsource: $$manifest:$$i";\
                                                                                ;;\
									esac;\
								fi;\
							done;\
						;;\
						*)\
							echo "Error: Invalid type '$$type' for manifest entry";\
							exit 1;\
						;;\
					esac;\
				;;\
				script)\
					cd $$manifest;\
					$(SHELL) $$type;\
				;;\
				*)\
					echo "Error: Invalid manifest entry type '$$entrytype;";\
					exit 1;\
				;;\
			esac;\
		fi;\
	done
